//
//  WinListView.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2020/2/10.
//  Copyright © 2020 zwb. All rights reserved.
//

import UIKit

class WinListView: UIView {
    
    
    @IBOutlet weak var titleLab: UITextField!
    weak var lastLab:oneView?
    var unitNm:String?
    var enterType = EnterType.none
    var tbList:[TbInfo]? {
        didSet {
            
            guard tbList.isSome && tbList!.count > 0 else {
                let lab = UILabel()
                lab.text = ""
                self.addSubview(lab)
                lab.snp.makeConstraints { (make) in
                    make.left.right.top.bottom.equalTo(self)
                }
                return
            }
            for i in 0..<tbList!.count {
                let item = oneView()
                var sstr = ""
                if tbList![i].tbInfoNew.unwrap.isEmpty {
                    var phoneDesc = ""
                    if self.enterType == .none {
                        phoneDesc = "电话：\(tbList![i].zTel.unwrap.replacePhone())"
                    } else {
                        phoneDesc = "电话：\(tbList![i].zTel.unwrap)"
                    }
                    sstr = "单位：\(tbList![i].zTbCompany.unwrap)\n价格：\(tbList![i].zPrice.unwrap)\n数量：\(tbList![i].zZbNum.unwrap)\n联系人：\(tbList![i].zName.unwrap)\n\(phoneDesc)"
              
                } else {
                    sstr = tbList![i].tbInfoNew.unwrap.replacingOccurrences(of: "#", with: "\n")

                }
 
                let paraph = NSMutableParagraphStyle()
                //将行间距设置为28
                paraph.lineSpacing = 4
                //样式属性集合
                let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
                                  NSAttributedString.Key.paragraphStyle: paraph]
                item.lab1.attributedText = NSAttributedString(string: sstr, attributes: attributes)
                

                self.addSubview(item)
                item.snp.makeConstraints { (make) in
                    make.left.equalTo(self).offset(25)
                    make.right.equalTo(self).offset(-25)
                    //                    make.height.equalTo(48)
                    if i == 0 {
                        make.top.equalTo(self).offset(40)
                    } else {
                        make.top.equalTo(lastLab!.snp.bottom).offset(8)
                    }
                    if i == (tbList!.count - 1) {
                        make.bottom.equalTo(self).offset(-20)
                    }
                    
                }
                lastLab = item
            }
            
        }
    }
    
    
    
}

class oneView:UIView {
    var lab1:UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let top = UIView()
        top.backgroundColor = #colorLiteral(red: 0.7764705882, green: 0.7764705882, blue: 0.7843137255, alpha: 1)
        let bottom = UIView()
        bottom.backgroundColor = #colorLiteral(red: 0.7764705882, green: 0.7764705882, blue: 0.7843137255, alpha: 1)
        let left = UIView()
        left.backgroundColor = #colorLiteral(red: 0.7764705882, green: 0.7764705882, blue: 0.7843137255, alpha: 1)
        let right = UIView()
        right.backgroundColor = #colorLiteral(red: 0.7764705882, green: 0.7764705882, blue: 0.7843137255, alpha: 1)
        self.addSubview(top)
        self.addSubview(bottom)
        self.addSubview(left)
        self.addSubview(right)
        top.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(self)
            make.height.equalTo(1)
        }
        bottom.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(1)
        }
        left.snp.makeConstraints { (make) in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(1)
        }
        right.snp.makeConstraints { (make) in
            make.top.right.bottom.equalTo(self)
            make.width.equalTo(1)
        }
        
        lab1 = UILabel()
        lab1.numberOfLines = 0
        lab1.font = .systemFont(ofSize: 15)
         self.addSubview(lab1)
     
 
        lab1.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(5)
            make.right.equalTo(self).offset(-5)
            make.top.equalTo(self).offset(5)
            make.bottom.equalTo(self).offset(-5)
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
